package com.tianma315.web.trace.controller;


import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

import com.tianma315.core.common.utils.Result;
import com.tianma315.core.constant.StateEnum;
import com.tianma315.core.procedures.domain.ProcedureLinkDO;
import com.tianma315.core.procedures.domain.ProcedureLinkVO;
import com.tianma315.core.trace.domain.TraceabilityFileDO;
import com.tianma315.core.trace.service.TraceOutService;
import com.tianma315.core.trace.service.TraceabilityFileService;
import com.tianma315.core.trace.vo.TraceabilityFileVO;
import com.tianma315.core.tymenu.domain.TyMenuDO;
import com.tianma315.core.tymenu.service.TyMenuService;
import com.tianma315.web.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <pre>
 * 溯源档案
 * </pre>
 * <small> 2020-02-25 13:54:23 | Aron</small>
 */
@Controller
@RequestMapping("/trace/traceabilityFile")
public class TraceabilityFileController extends BaseController {
	@Autowired
	private TraceabilityFileService traceabilityFileService;
	@Autowired
	private TraceOutService traceOutService;
	@Autowired
	private TyMenuService tyMenuService;
	
	@GetMapping()
	@RequiresPermissions("trace:traceabilityFile:traceabilityFile")
	String TraceabilityFile(){
	    return "trace/traceabilityFile/traceabilityFile";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("trace:traceabilityFile:traceabilityFile")
	public Result<Page<TraceabilityFileVO>> list(Integer pageNumber, Integer pageSize, TraceabilityFileDO traceabilityFileDTO){
		// 查询列表数据
//        Page<TraceabilityFileDO> page = new Page<>(pageNumber, pageSize);
//
//        Wrapper<TraceabilityFileDO> wrapper = new EntityWrapper<TraceabilityFileDO>(traceabilityFileDTO);
//        page = traceabilityFileService.selectPage(page, wrapper);
//        int total = traceabilityFileService.selectCount(wrapper);
//        page.setTotal(total);
		Page<TraceabilityFileVO> page = traceabilityFileService.getTraceabilityFilePage(pageNumber,pageSize,traceabilityFileDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("trace:traceabilityFile:add")
	String add(){
	    return "trace/traceabilityFile/add";
	}

	@GetMapping("/edit/{traceabilityFileId}")
	@RequiresPermissions("trace:traceabilityFile:edit")
	String edit(@PathVariable("traceabilityFileId") Integer traceabilityFileId,Model model){
		TraceabilityFileDO traceabilityFile = traceabilityFileService.selectById(traceabilityFileId);
		model.addAttribute("traceabilityFile", traceabilityFile);
	    return "trace/traceabilityFile/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("trace:traceabilityFile:add")
	public Result<String> save( TraceabilityFileDO traceabilityFile){
		traceabilityFile.setMerchantId(getMerchantId());
		traceabilityFile.setCreateBy(getUserId());
		traceabilityFile.setCreateDate(Calendar.getInstance().getTime());
		traceabilityFile.setState(StateEnum.not_del.getCode());
		if(traceabilityFileService.insert(traceabilityFile)){
			return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 保存
	 */
	@GetMapping("/addTraceOut/{TraceabilityFileId}/{productId}")
	@RequiresPermissions("trace:traceOut:add")
	public String save(@PathVariable("productId") Integer productId,@PathVariable("TraceabilityFileId") Integer traceabilityFileId,Model model){

		List<ProcedureLinkVO> contentList=traceOutService.selectContent(productId,traceabilityFileId);
		EntityWrapper<TyMenuDO> wrapper = new EntityWrapper<>();
		wrapper.eq("traceability_file_id",traceabilityFileId)
		.eq("is_delete",0)
		.eq("menu_level",0);
		List<TyMenuDO> tyMenuDOS = tyMenuService.selectList(wrapper);

		model.addAttribute("traceabilityFileId", traceabilityFileId);
		model.addAttribute("productId", productId);

		//判断一级环节是否全部已录入
		if (tyMenuDOS.size()==0||contentList.size()!=tyMenuDOS.size()){
			model.addAttribute("contentList", contentList);
			model.addAttribute("msg", "请完善未录入的信息");
			return "trace/traceOut/toOutContent";
		}
		return "trace/traceOut/add";
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("trace:traceabilityFile:edit")
	public Result<String>  update( TraceabilityFileDO traceabilityFile){
		traceabilityFileService.updateById(traceabilityFile);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("trace:traceabilityFile:remove")
	public Result<String>  remove( Integer traceabilityFileId){
		if(traceabilityFileService.updateStateById(traceabilityFileId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("trace:traceabilityFile:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] traceabilityFileIds){
		for (Integer id : traceabilityFileIds){
			traceabilityFileService.updateStateById(id);
		}
		return Result.ok();
	}

	@GetMapping("/all")
	@ResponseBody
	public Result<List<TraceabilityFileDO>> all(){
		List<TraceabilityFileDO> TraceabilityFileDOs = traceabilityFileService.getTraceabilityFileDOList();
		return Result.ok(TraceabilityFileDOs);
	}

	@GetMapping("/getDataByTraceabilityFileId/{TraceabilityFileId}/{productId}")
	public ModelAndView getDataByTraceabilityFileId(@PathVariable("TraceabilityFileId") Integer traceabilityFileId,@PathVariable("productId") Integer productId) throws Exception {

		ModelAndView model = new ModelAndView("trace/traceabilityFile/result");

		List<ProcedureLinkVO> contentList=traceOutService.selectContent(productId,traceabilityFileId);
		EntityWrapper<TyMenuDO> wrapper = new EntityWrapper<>();
		wrapper.eq("traceability_file_id",traceabilityFileId)
		.eq("is_delete",0)
		.eq("menu_level",0);
		List<TyMenuDO> tyMenuDOS = tyMenuService.selectList(wrapper);
		model.addObject("traceabilityFileId", traceabilityFileId);
		//判断一级环节是否全部已录入
		if (tyMenuDOS.size()==0||contentList.size()!=tyMenuDOS.size()){
			model.addObject("contentList", contentList);
			model.addObject("msg", "请完善未录入的信息");
			model.setViewName("trace/traceOut/toOutContent");
			return model;
		}
		JSONObject result = traceabilityFileService.getDataByTraceabilityFileId(traceabilityFileId);
		result.put("reportApiVO",null);
		result.put("scanRecord",null);
		model.addObject("result",result);
		return model;
	}
}
